package com.zimmur.solr.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zimmur.solr.SolrUtil;
import com.zimmur.solr.common.SolrData;
import com.zimmur.solr.model.Goods;
import com.zimmur.solr.model.SearchableGoods;
import com.zimmur.solr.model.SearchableTopic;
import com.zimmur.solr.model.Topic;
import com.zimmur.solr.service.ISolrTopicService;
import com.zimmur.tools.PageUtil;
@Service("solrTopicService")
public class SolrTopicServiceImpl implements ISolrTopicService{
	Logger log=LoggerFactory.getLogger(SolrTopicServiceImpl.class);
	@Resource
	private SolrUtil<Topic> solrUtil;
	
	@Override
	public int deleteById(String topicId){
		int flag=1;
		UpdateResponse updateResponse;
		try {
			updateResponse = solrUtil.deleteById(topicId, SolrData.CORE_TOPIC);
			flag=updateResponse.getStatus();
			solrUtil.commit(SolrData.CORE_TOPIC);
		} catch (SolrServerException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_TOPIC);
			flag=-1;
		} catch (IOException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_TOPIC);
			flag=-1;
		}
		return flag;
	}
	@Override
	public int add(Topic topic){
		int flag=1;
		UpdateResponse updateResponse;
		try {
			updateResponse = solrUtil.add(topic, SolrData.CORE_TOPIC);
			flag=updateResponse.getStatus();
			solrUtil.commit(SolrData.CORE_TOPIC);
		} catch (SolrServerException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_TOPIC);
			flag=-1;
		} catch (IOException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_TOPIC);
			flag=-1;
		}
		return flag;
	}
	@Override
	public PageUtil<Topic> queryTopic(String keyword, int pageNo) {	
		PageUtil<Topic> page=new PageUtil<>();
		List<Topic> topicList=null;
		String q=SearchableTopic.ISAPPLY_FIELD+":"+"1";
		List<String> fqList=new ArrayList<>();
		
		if(!StringUtils.isEmpty(keyword)){
			fqList.add(SearchableTopic.SEARCH_KEYWORD+":"+keyword);
		}
		String fq[]=null;
		if(fqList.size()>0)
			fq=fqList.toArray(new String[fqList.size()]);
		String  orderByClause= SearchableTopic.CREATEDATE_FIELD +" desc";
		try {
			QueryResponse queryResponse=solrUtil.query(q, fq, orderByClause, pageNo, SolrData.CORE_TOPIC);
			topicList=queryResponse.getBeans(Topic.class);
			int totalFound = (int)queryResponse.getResults().getNumFound();
			page.setTotalRecord(totalFound);
			page.setItemList(topicList);
		} catch (SolrServerException e) {
			log.error(e.getMessage(),e);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return page;
	}
	@Override
	public int addlist(List<Topic> topics) {
		int flag=1;
		UpdateResponse updateResponse;
		try {
			updateResponse = solrUtil.addList(topics, SolrData.CORE_TOPIC);
			flag=updateResponse.getStatus();
			solrUtil.commit(SolrData.CORE_TOPIC);
		} catch (SolrServerException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_TOPIC);
			flag=-1;
		} catch (IOException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_TOPIC);
			flag=-1;
		}
		return flag;
	}

}
